package com.yonyou.iuap.ypd.bizflow;

import com.yonyou.ypd.bill.annotation.BillPlugin;
import com.yonyou.ypd.bill.basic.constant.BillActionEnum;
import com.yonyou.ypd.bill.bizflow.service.api.IYpdBillBizFlowService;
import com.yonyou.ypd.bill.context.YpdBillContext;
import com.yonyou.ypd.bill.plugin.AbstractBillPlugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author: zhuyjh
 * @Date: 2023/01/09 9:55
 */
@BillPlugin(busiObj = "tz111")
public class BizflowPlugin extends AbstractBillPlugin {

    @Autowired(required = false)
    private IYpdBillBizFlowService bizFlowService;

    private static Logger log = LoggerFactory.getLogger(BizflowPlugin.class);

    @Override
    public void afterSave(YpdBillContext billContext) throws Exception {
        try {
            bizFlowService.push(billContext.getBaseBillContext(), billContext.getBillDO());
        }catch(Exception e){
            log.error("afterSave 推单失败");
        }
        try {
            bizFlowService.writeBack(billContext.getBaseBillContext(),billContext.getBillDO());
        }catch(Exception e){
            log.error("afterSave 回写失败");
        }
    }


    @Override
    public void beforeDelete(YpdBillContext billContext) throws Exception {
        try {
            bizFlowService.push(billContext.getBaseBillContext(), billContext.getBillDO());
        }catch(Exception e){
            log.error("beforeDelete 推单失败");
        }
        try {
            bizFlowService.writeBack(billContext.getBaseBillContext(),billContext.getBillDO());
        }catch(Exception e){
            log.error("beforeDelete 回写失败");
        }
    }

    @Override
    public void beforeAudit(YpdBillContext billContext) throws Exception {
        try {
            bizFlowService.push(billContext.getBaseBillContext(), billContext.getBillDO());
        }catch(Exception e){
            log.error("beforeAudit 推单失败");
        }
        try {
            bizFlowService.writeBack(billContext.getBaseBillContext(),billContext.getBillDO());
        }catch(Exception e){
            log.error("beforeAudit 回写失败");
        }
    }

    @Override
    public void beforeUnAudit(YpdBillContext billContext) throws Exception {
        try {
            bizFlowService.push(billContext.getBaseBillContext(), billContext.getBillDO());
        }catch(Exception e){
            log.error("beforeUnAudit 推单失败");
        }
        try {
            bizFlowService.writeBack(billContext.getBaseBillContext(),billContext.getBillDO());
        }catch(Exception e){
            log.error("beforeUnAudit 回写失败");
        }
    }


    @Override
    public void beforeUnSubmit(YpdBillContext billContext) throws Exception {
        try {
            bizFlowService.push(billContext.getBaseBillContext(), billContext.getBillDO());
        }catch(Exception e){
            log.error("beforeUnSubmit 推单失败");
        }
        try {
            bizFlowService.writeBack(billContext.getBaseBillContext(),billContext.getBillDO());
        }catch(Exception e){
            log.error("beforeUnSubmit 回写失败");
        }
    }

    @Override
    public void afterBpmActivitycomplete(YpdBillContext billContext) throws Exception {
        try {
            bizFlowService.push(billContext.getBaseBillContext(), billContext.getBillDO(), BillActionEnum.ACTIONCODE_BPM_ACTIVITY_COMPLETE, true);
        }catch(Exception e){
            log.error("afterBpmActivitycomplete 推单失败");
        }
        try {
            bizFlowService.writeBack(billContext.getBaseBillContext(), billContext.getBillDO(), BillActionEnum.ACTIONCODE_BPM_ACTIVITY_COMPLETE);
        }catch(Exception e){
            log.error("afterBpmActivitycomplete 回写失败");
        }
    }


    @Override
    public void afterBpmcomplete(YpdBillContext billContext) throws Exception {
        try {
            bizFlowService.push(billContext.getBaseBillContext(), billContext.getBillDO(), BillActionEnum.ACTIONCODE_BPMCOMPLETE, true);
        }catch(Exception e){
            log.error("afterBpmcomplete 推单失败");
        }
        try {
            bizFlowService.writeBack(billContext.getBaseBillContext(), billContext.getBillDO(), BillActionEnum.ACTIONCODE_BPMCOMPLETE);
        }catch(Exception e){
            log.error("afterBpmcomplete 回写失败");
        }
    }

}